<?php 
	$txtcode=replatestring($_POST['txtcode'],false);
	$txtHoten=replatestring($_POST['txtHoten'],false);
	$khachhang=replatestring($_POST['Kha_Id'],false);
	$kh_id=$_POST['kh_id'];
	if($khachhang==""){$khachhang=$kh_id;}
	
	$ngayban=replatestring($_POST['ngayban'],false);
	$giatri=$_POST['giatri'];

	$donviquanly=replatestring($_POST['donviquanly'],false);
	$nguoiql=$_POST['nql'];
	$txtNguoiquanly=replatestring($_POST['txtNguoiquanly'],false);
	if($txtNguoiquanly==""){$txtNguoiquanly=$nguoiql;}
	$nguoiqlk=$_POST['nguoiqlk'];
	$nguoitheodoi=replatestring($_POST['Nguoi_Quan_Ly_Khac'],false);
	if($nguoitheodoi==""){$nguoitheodoi=$nguoiqlk;}
	$nguoitao=replatestring($_SESSION['userid'],false);
	$ngaytao=time();
	$nguoisua=$_SESSION['userid'];
	$tgsua=time();
	$descripton=$_POST['txtnoidung'];
	
?>

<?php 
$id_dh=replatestring($_POST['id_dh'],false);
$data=false;
if($txtcode!=="" and $txtHoten!==""){$data=true;}
if($data==true){
	if($id_dh=="")
	{
	mysql_query("insert into donhang
	(code,name,kh_id,ngayban,manager_unit,manager,manager_other,creater,creat_time,user_edit,update_time,description,giatri)
value('$txtcode','$txtHoten','$khachhang','$ngayban','$donviquanly','$txtNguoiquanly','$nguoitheodoi','$nguoitao','$ngaytao','$nguoisua','$tgsua','$descripton','$giatri')
				");
	
	
		$dh_id=mysql_insert_id();
		//hopdong_ct
		$id_row=$_POST['id_row'];
		$record=$_POST['id_record'];
		$record2=json_encode($record);
		$record3=explode(',',$record2);
		
		$Cls_Id[]=$_POST['clsidarr'];
		$lsp= json_encode($Cls_Id);
		$lsp2=explode(',',$lsp);
		
		$Sph_Id[]=$_POST['sphidarr'];
		$sp=json_encode($Sph_Id);
		$sp2=explode(',',$sp);
		
		$Dvt_Id[]=$_POST['donvitinharr'];
		$dv=json_encode($Dvt_Id);
		$dv2=explode(',',$dv);
		
		$giaban[]=$_POST['giabanarr'];
		$giaban2= json_encode($giaban);
		$giaban3=explode(',',$giaban2);
		
		$soluong[]=$_POST['soluongarr'];
		$soluong1= json_encode($soluong);
		$soluong2=explode(',',$soluong1);
		
		$tong_tien[]=$_POST['tongtienarr'];
		$tongtien1= json_encode($tong_tien);
		$tongtien2=explode(',',$tongtien1);
		
		$vat[]=$_POST['vatarr'];
		$vat1= json_encode($vat);
		$vat2=explode(',',$vat1);
		
		$tienvat[]=$_POST['tienvatarr'];
		$tienvat1= json_encode($tienvat);
		$tienvat2=explode(',',$tienvat1);
		
		$thanhtien[]=$_POST['thanhtienarr'];
		$thanhtien1= json_encode($thanhtien);
		$thanhtien2=explode(',',$thanhtien1);
		for($n=1;$n<=$id_row;$n++)
				{		
				$record_id=$record3[$n-1];
				$record_id=preg_replace('/[^0-9]/','',$record_id);
				
				$lsp3=$lsp2[$n-1];
				$lsp3=preg_replace('/[^0-9]/','',$lsp3);
				
				$sp3=$sp2[$n-1];
				$sp3=preg_replace('/[^0-9]/','',$sp3);
				
				$dv3=$dv2[$n-1];
				$dv3=preg_replace('/[^0-9]/','',$dv3);
				
				$gbId=$giaban3[$n-1];
				$gbId=preg_replace('/[^0-9]/','',$gbId);
				
				$sl=$soluong2[$n-1];
				$sl=preg_replace('/[^0-9]/','',$sl);
				
				$tong=$tongtien2[$n-1];
				$tong=preg_replace('/[^0-9]/','',$tong);
				
				$vatid=$vat2[$n-1];
				$vatid=preg_replace('/[^0-9]/','',$vatid);
				
				$tienvatid=$tienvat2[$n-1];
				$tienvatid=preg_replace('/[^0-9]/','',$tienvatid);
				
				$tt=$thanhtien2[$n-1];
				$tt=preg_replace('/[^0-9]/','',$tt);
					mysql_query("insert into donhang_ct(donhang_id,lsp,sp,dv,dongia,soluong,cong,vat,tienvat,thanhtien,id_row) 		 	
								 value('$dh_id','$lsp3','$sp3','$dv3','$gbId','$sl','$tong','$vatid','$tienvatid','$tt','$record_id')");
				}
		$cauthongbao="Thêm thành công";
		$link=$url_main_site."system/index.php?tn=contract&hd=order_list";
		thongbao($cauthongbao,$link);
	}
	else
	{
		mysql_query("
					update donhang set
						code = '$txtcode',
						name = '$txtHoten',
						kh_id  = '$khachhang',
						ngayban = '$ngayban',
						manager_unit = '$donviquanly',
						manager = '$txtNguoiquanly',
						manager_other ='$nguoitheodoi',
						creater ='$nguoitao',
						creat_time = '$ngaytao',
						user_edit ='$nguoisua',
						update_time ='$tgsua',
						description ='$descripton'
					where id ='$id_dh'
						
		");
		
			$cauthongbao="Bạn đã sửa thành công";
			$link=$url_main_site."system/index.php?tn=contract&hd=order_list";
			thongbao($cauthongbao,$link);
	}
}
else
{
		$cauthongbao="Dữ liệu không hợp lệ";
		$link=$url_main_site."system/index.php?tn=contract&hd=order_list";
		thongbao($cauthongbao,$link);
}
?>